using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace TurbaconPhone
{
    class Particle : GameObject
    {
        #region Fields
        Vector2 position;
        Vector2 velocity;
        Vector2 acceleration;

        float orientation;
        float angularVelocity;

        float scale;
        float RescaleFactor;

        float LifeTime, Timer;
        #endregion

        #region Properties
        public override Vector2 Pos
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public override Vector2 Vel
        {
            get
            {
                return velocity;
            }
            set
            {
                velocity = value;
            }
        }

        public override float Angle
        {
            get
            {
                return orientation;
            }
            set
            {
                orientation = value;
            }
        }

        public override float AngVel
        {
            get
            {
                return angularVelocity;
            }
            set
            {
                angularVelocity = value;
            }
        }

        public override float Scale
        {
            get
            {
                return scale;
            }
            set
            {
                scale = value;
            }
        }

        public bool FadeIO
        {
            get;
            set;
        }

        #endregion

        #region Constructors
        public Particle(Texture2D texture, Vector2 position, Vector2 velocity, float lifeTime)
            : base(texture)
        {
            Pos = position;
            Vel = velocity;
            acceleration = Vector2.Zero;
            
            Angle = AngVel = 0f;
           
            Scale = RescaleFactor = 1f;
            
            LifeTime = Timer = lifeTime;
            Color = new Color(1f,1f,1f,0f);
            FadeIO = false;
        }
        #endregion

        #region Update
        public override void Update(GameTime gameTime)
        {
            Timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (Timer <= 0) Die();

            Pos += Vel;
            Vel += acceleration;

            Angle += AngVel;

            Scale *= RescaleFactor;

            if (FadeIO) Color = FadeInFadeOut();

            base.Update(gameTime);
        }
        #endregion

        public void Accelerating(Vector2 accel)
        {
            acceleration = accel;
        }

        public void Rotating(float startOrientation, float angularVelocity)
        {
            Angle = startOrientation;
            AngVel = angularVelocity;
        }

        public void Resizing(float startSize, float rescaleFactor)
        {
            Scale = startSize;
            RescaleFactor = rescaleFactor;
        }

        private Color FadeInFadeOut()
        {
            float alpha = 4 * (Timer / LifeTime) * (1  - (Timer / LifeTime));
            return new Color(1f,1f,1f,0f) * alpha;
        }
    }
}
